package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.fragment.PrivateInfoFragment;
import com.kkd.kkdapp.fragment.PublicInfoFragment;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;

import android.animation.ObjectAnimator;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 消息中心
 * 
 * @author mty
 * @rebuild by zhaochuang 20160525
 */
public class InformationActivity extends BaseActivity {

	public static int mCurrIndex = 0;// 当前页卡编号
	private RelativeLayout mBackRl; // 返回键
	private ImageView mCursor;// 动画图片
	private ViewPager mViewPager;
	private FragmentAdapter mAdapter;
	private TextView personage;
	private TextView system;
	private RelativeLayout personage_rl;
	private RelativeLayout system_rl;
	// #############修改 消息中心-个人消息，无记录时，不需要显示“全部已读”
	public static TextView mReadedTv;
	// ####################
	private PublicInfoFragment mPublicInfoFragment; // 公共消息，系统消息
	private PrivateInfoFragment mPrivateInfoFragment;// 私有消息，个人消息
	public TextView dot; // 个人消息红点
	public TextView system_dot; // 系统消息小红点
	private int screenW; // 屏幕宽度
	private ObjectAnimator translation; // 平移动画

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_information);
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mViewPager.setCurrentItem(mCurrIndex);
	}

	/**
	 * 
	 * 初始化组件
	 */
	public void initView() {
		system_rl = (RelativeLayout) this.findViewById(R.id.system_rl);
		personage_rl = (RelativeLayout) this.findViewById(R.id.personage_rl);
		personage = (TextView) this.findViewById(R.id.personage);
		system = (TextView) this.findViewById(R.id.system);
		mBackRl = (RelativeLayout) this.findViewById(R.id.back_rl);
		mViewPager = (ViewPager) this.findViewById(R.id.viewpager);
		mCursor = (ImageView) this.findViewById(R.id.tab_scroll_img);
		mReadedTv = (TextView) this.findViewById(R.id.readed);
		dot = (TextView) this.findViewById(R.id.dot);
		system_dot = (TextView) this.findViewById(R.id.system_dot);
		mAdapter = new FragmentAdapter(getSupportFragmentManager());
		mViewPager.setAdapter(mAdapter);
		if (PreferenceUtils.getInstance().getSystemInformation(
				InformationActivity.this)) {
			system_dot.setVisibility(View.GONE);
		}
		screenW = SystemUtils.getScreenRealWidth(this);// 获取分辨率宽度
		mReadedTv.setOnClickListener(mOnSingleClickListener);
		system_rl.setOnClickListener(mOnSingleClickListener);
		personage_rl.setOnClickListener(mOnSingleClickListener);
		mBackRl.setOnClickListener(mOnSingleClickListener);
		mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());

	}

	OnSingleClickListener mOnSingleClickListener = new OnSingleClickListener() {

		@Override
		public void onSingleClick(View v) {
			switch (v.getId()) {
			case R.id.back_rl:
				PreferenceUtils.getInstance().setMyproperty(
						InformationActivity.this, "");
				finish();
				break;
			case R.id.personage_rl:
				personageState();
				break;
			case R.id.system_rl:
				SystemUtils.onEvent("Account_Massage_SystemMessage", 1);
				systemState();
				break;
			case R.id.readed:
				if (mViewPager.getCurrentItem() == 1) {
					if (mPublicInfoFragment.getSystemcreateTime() != -1) {
						if (mPublicInfoFragment.pageNO == 1) {
							PreferenceUtils.getInstance().setAllreaded(
									InformationActivity.this,
									mPublicInfoFragment.getSystemcreateTime());
						}
					}
					mPublicInfoFragment.pageNO = 1;
					mPublicInfoFragment.setIsToAllReaded();
					mPublicInfoFragment.queryNotificationMsgList();
				}
				break;
			}

		}
	};

	/**
	 * 个人消息
	 */
	public void personageState() {
		mReadedTv.setVisibility(View.GONE);
		mViewPager.setCurrentItem(0);
		personage.setTextColor(this.getResources()
				.getColor(R.color.blue_5451b1));
		system.setTextColor(Color.parseColor("#505050"));
	}

	/**
	 * 系统消息
	 */
	public void systemState() {
		mReadedTv.setVisibility(View.VISIBLE);
		mViewPager.setCurrentItem(1);
		personage.setTextColor(Color.parseColor("#505050"));
		system.setTextColor(this.getResources().getColor(R.color.blue_5451b1));
	}

	/**
	 * 页卡切换监听
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int index) {
			Animation animation = null;
			switch (index) {
			case 0:
				mPrivateInfoFragment.pageNO = 1;
				mPrivateInfoFragment.questInformationManager();
				personageState();
				imgTranslation(screenW / 2, 0);
				break;
			case 1:
				PreferenceUtils.getInstance().setSystemInformation(
						InformationActivity.this, true);
				if (PreferenceUtils.getInstance().getSystemInformation(
						InformationActivity.this)) {
					system_dot.setVisibility(View.GONE);
				}
				mPublicInfoFragment.pageNO = 1;
				mPublicInfoFragment.queryNotificationMsgList();
				systemState();
				imgTranslation(0, screenW / 2);
				break;
			}
			mCurrIndex = index;
			// cancelTranslation();
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}
	}

	private class FragmentAdapter extends FragmentPagerAdapter {

		List<Fragment> list;

		public FragmentAdapter(FragmentManager fm) {
			super(fm);
			mPrivateInfoFragment = PrivateInfoFragment.newInstance();
			mPublicInfoFragment = PublicInfoFragment.newInstance();
			list = new ArrayList<Fragment>();
			list.add(mPrivateInfoFragment);
			list.add(mPublicInfoFragment);
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				return list.get(0);
			case 1:
				return list.get(1);
			}
			return null;
		}

		@Override
		public int getCount() {
			return 2;
		}
	}

	// 平移动画
	public void imgTranslation(float translaA, float translaB) {
		if (translation != null) {
			translation.cancel();
			translation = null;
		}
		translation = ObjectAnimator.ofFloat(mCursor, "translationX", translaA,
				translaB);
		translation.setDuration(200);
		translation.setInterpolator(new AccelerateDecelerateInterpolator());
		translation.start();
	}

	// 关闭动画
	public void cancelTranslation() {
		if (translation != null) {
			translation.cancel();
			translation = null;
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		cancelTranslation();
	}
}